package com.pap.msg.message.service.impl;

import com.pap.base.util.date.DateUtils;
import com.pap.beans.idworker.IdWorker;
import com.pap.msg.dto.MsgContentUserDetailsDTO;
import com.pap.msg.message.entity.MsgContent;
import com.pap.msg.message.entity.MsgInfo;
import com.pap.msg.message.mapper.MsgContentMapper;
import com.pap.msg.message.mapper.MsgInfoMapper;
import com.pap.msg.message.service.IMsgInfoService;
import com.pap.obj.vo.logineduser.LoginedUserVO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Transactional
@Service("msgInfoService")
public class MsgInfoServiceImpl implements IMsgInfoService {

    @Resource(name = "msgInfoMapper")
    private MsgInfoMapper mapper;

    @Resource(name = "msgContentMapper")
    private MsgContentMapper msgContentMapper;

    @Resource(name = "idWorker")
    private IdWorker idWorker;

    @Override
    public int deleteByPrimaryKey(String id) {
       return mapper.deleteByPrimaryKey(id);
    }

    @Override
    public int selectCountByMap(Map<Object, Object> map) {
      return mapper.selectCountByMap(map);
    }

    @Override
    public List<MsgInfo> selectListByMap(Map<Object, Object> map) {
        return mapper.selectListByMap(map);
    }

    @Override
    public int insert(MsgInfo record) {
       return mapper.insert(record);
    }

    @Override
    public int insertSelective(MsgInfo record) {
       return mapper.insertSelective(record);
    }

    @Override
    public MsgInfo selectByPrimaryKey(String id) {
       return mapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(MsgInfo record) {
       return mapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(MsgInfo record) {
      return mapper.updateByPrimaryKey(record);
    }

    @Override
    public String createSysMsgFromMsgContent(String userReceiverId, LoginedUserVO loginedUserVO) {
        StringBuffer returnMsgInfoIds = new StringBuffer(" ");

        Map<Object, Object> userReceivedIdMap = new HashMap<Object, Object>(2);
        userReceivedIdMap.put("userReceiverId", userReceiverId);
        userReceivedIdMap.put("orderBySqlJudgeStr", "msg_content_id desc");
        List<MsgInfo> msgContentIdList =  mapper.selectListByMap(userReceivedIdMap);

        String newestMsgContentId = "0";
        if(msgContentIdList != null && msgContentIdList.size() > 0) {
            newestMsgContentId = msgContentIdList.get(0).getMsgContentId();
        }

        // 系统级别的，未被同步的消息集合实体
        Map<Object, Object> msgContentMap = new HashMap<Object, Object>(2);
        msgContentMap.put("myGreater_msgContentId", newestMsgContentId);
        msgContentMap.put("msgContentType", "S");
        List<MsgContent> msgContents = msgContentMapper.selectListByMap(msgContentMap);

        // msgContents 系统级别的，未被同步的消息集合实体， 放置到消息容器中
        if(msgContents != null && msgContents.size() > 0) {
            for (MsgContent msgContentTmp : msgContents) {
                MsgInfo msgInfo = new MsgInfo();
                String idStr = idWorker.nextIdStr();
                returnMsgInfoIds.append(idStr).append(",");
                msgInfo.setMsgInfoId(idStr);
                msgInfo.setMsgContentId(msgContentTmp.getMsgContentId());
                msgInfo.setUserSendId("system");
                msgInfo.setUserSendName("system");
                msgInfo.setUserReceiverId(userReceiverId);
                msgInfo.setUssrReceiverName("");
                msgInfo.setReadFlag("N");
                msgInfo.setCreateUser("system");
                msgInfo.setCreateTime(DateUtils.getCurrDateTimeStr());
                msgInfo.setCreateIp("0.0.0.1");
                msgInfo.setClientLicenseId(loginedUserVO.getClientLicenseId());
                mapper.insertSelective(msgInfo);

            }
        }

        return returnMsgInfoIds.substring(0, returnMsgInfoIds.length() - 1).toString().trim();
    }

    @Override
    public void createMsgInfoByMsgContentUserIds(MsgContentUserDetailsDTO msgContentUserDetailsDTO, LoginedUserVO loginedUserVO) {
        List<String> userReceiverIdList = msgContentUserDetailsDTO.getUserReceiverIds();
        if (userReceiverIdList != null && userReceiverIdList.size() > 0) {
            for(String userReceiverId: userReceiverIdList) {
                MsgInfo msgInfo = new MsgInfo();
                String idStr = idWorker.nextIdStr();
                msgInfo.setMsgInfoId(idStr);
                msgInfo.setMsgContentId(msgContentUserDetailsDTO.getMsgContentId());
                msgInfo.setUserSendId("system");
                msgInfo.setUserSendName("system");
                msgInfo.setUserReceiverId(userReceiverId);
                msgInfo.setUssrReceiverName("");
                msgInfo.setReadFlag("N");
                msgInfo.setCreateUser(loginedUserVO.getName());
                msgInfo.setCreateTime(DateUtils.getCurrDateTimeStr());
                msgInfo.setCreateIp("0.0.0.1");
                msgInfo.setClientLicenseId(loginedUserVO.getClientLicenseId());
                mapper.insertSelective(msgInfo);
            }
        }
        // 消息状态变为已发送
        MsgContent msgContent = new MsgContent();
        msgContent.setMsgContentId(msgContentUserDetailsDTO.getMsgContentId());
        msgContent.setMsgStatus("P");
        msgContentMapper.updateByPrimaryKeySelective(msgContent);
    }
}